import datetime

from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse, FileResponse,HttpResponseRedirect
from django.template.loader import render_to_string
# Create your views here.
from django.urls import reverse
from django.views.generic import TemplateView


def hello_world(request):
    return HttpResponse('hello world')


def hello_china(request):
    # raise
    return HttpResponse('hello china')


def hello_html(request):
    """响应html内容"""
    html ="""
    <html>
        <body>
            <h1 style="color:#f00"> hello html </h1>
        </body>
    </html>
    """
    return HttpResponse(html)


def article_list(request, month):
    """

    :param request:
    :param month: 某个月份文章列表，获取的url参数
    :return:
    """
    return HttpResponse('article:{}'.format(month))


def search(request):
    """GET参数获取"""
    name = request.GET.get('name', '')
    print(name)
    return HttpResponse('查询成功')


def test_34(request, num):
    if int(num) % 2 == 0:
        return HttpResponse('{}是偶数'.format(num))
    else:
        return HttpResponse('{}不是偶数'.format(num))


def render_str(request):
    """ render_to_string 使用"""
    templ_name = 'index2.html'
    html = render_to_string(template_name=templ_name)
    return HttpResponse(html)


def render_html(request):
    """ render_to_string 使用"""
    return render(request, template_name='index.html')


def http_request(request):
    """请求练习"""
    # 1 请求方式
    print(request.method)
    # 2.1 请求头信息
    print(request.META)
    ua = request.META.get('HTTP_USER_AGENT', None)
    print(ua)
    # 2.2 更精简头部信息
    print(request.headers)
    print(request.headers['user-agent'])
    # 3 获取请求参数
    name = request.GET.get('name', '')
    print(name)
    return HttpResponse('响应')


def http_response(request):
    """响应练习"""
    # http
    resp = HttpResponse('响应内容')

    # json
    user_info = {
        'name': '郑',
        'age': '23'
    }
    resp = JsonResponse(user_info, json_dumps_params={'ensure_ascii': False})
    # file
    # resp = FileResponse(open('./templates/index2.html', 'rb'))
    return resp


def no_data_404(request):
    """404页面"""
    return HttpResponse('404')


def article_detail(request, article_id):
    """
    文章详情
    :param request:
    :param article_id: 1000开始整数
    :return:
    """
    if article_id < 1000:
        #return HttpResponseRedirect(reverse('404'))
        return redirect('404')
    return HttpResponse('文章{}的内容'.format(article_id))


class HomeView(TemplateView):
    template_name = 'home.html'


def index(request):
    username = 'zzy'
    age = 23
    list_users = [
        {'name': 'a', 'age': 34},
        {'name': 'b', 'age': 32},

    ]
    return render(request, 'index2.html', {
        'username': username,
        'age': age,
        'list_users': list_users
    })


def tag(request):
    list_users = [
        {'name': 'a', 'age': 34},
        {'name': 'b', 'age': 32},
        {'name': 'c', 'age': 34, 'sex': '男'},
        {'name': 'd', 'age': 32},
    ]
    # 触发empty
    # list_users = []
    return render(request, 'tag.html', {'list_users': list_users})


def test_filter(request):
    # 过滤器的使用
    name = 'model'
    list_user = []
    # user_info = None
    user_info = []
    pi = 3.1415926
    my_date = datetime.datetime(2021, 10, 24)
    html = '<h3>welcome to Python</h3>'
    return render(request, 'test_filter.html', {
        'name': name,
        'list_user': list_user,
        'user_info': user_info,
        'pi': pi,
        'my_date': my_date,
        'html': html
    })


def mine_filter(request):
    # 自定义过滤器
    user_list = ['张三', '欧阳山水', '李司丰']
    return render(request, 'mine_filter.html', {
        'user_list': user_list
    })
